package tools

import (
	"fmt"
	"io"
	"net/http"
	"os"
)

func DownloadFile(url, filename string) error {
	response, err := http.Get(url)
	if err != nil {
		return err
	}
	defer response.Body.Close()

	// 检查服务器响应状态
	if response.StatusCode != http.StatusOK {
		return fmt.Errorf("下载失败: %s", response.Status)
	}

	// 创建目标文件
	file, err := os.Create(filename)
	if err != nil {
		return err
	}
	// 将下载内容写入文件
	_, err = io.Copy(file, response.Body)
	if err != nil {
		return err
	}
	return nil
}
